package com.topfinance.test.cfg;

import java.io.*;
import java.sql.*;
import java.util.List;

import oracle.sql.CLOB;
import org.hibernate.*;
import org.hibernate.lob.*;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.core.io.ClassPathResource;

import com.topfinance.frame.exception.AppException;
import com.topfinance.frame.util.*;
import com.topfinance.payment.ebo.*;
import com.topfinance.payment.service.*;
import com.topfinance.payment.service.impl.*;

public class CfgTester {

	private static XmlBeanFactory f3 = null;

	private static void init() {
		if (f3 == null) {
			XmlBeanFactory f1 = new XmlBeanFactory(new ClassPathResource("spring/applicationContext.xml"));
			XmlBeanFactory f2 = new XmlBeanFactory(new ClassPathResource("spring/dataAccessContext-hibernate.xml"), f1);
			f3 = new XmlBeanFactory(new ClassPathResource("spring/serviceContext.xml"), f2);
		}
	}

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		long btime = System.currentTimeMillis();
		try {
			// testBeanUtil();
			init();

			//测试插入XML文件，并读出内容
			//路径根据实际需要修改
			testInitXMLFormat("E:\\Docs\\techDocs\\CNAPS2\\支付平台\\二代支付\\sql\\nets.356.001.01.xsd");
			
			//读出StringClobType
			//testReadXMLFormat();

		} catch (Exception e) {
			e.printStackTrace();
		}
		long etime = System.currentTimeMillis();
		System.out.println("[TOTAL]" + (etime - btime));
	}

	// 测试插入XML文件，并读出
	private static void testReadXMLFormat() throws AppException, Exception {
		ITCfgFormatMgr fmtImp = (TCfgFormatMgrImpl) f3.getBean("cfgFormatMgr");
		List<TCfgFmtXmlEbo> xmls = fmtImp.getAllFmtXML(null,"行内1_xml");
		for(TCfgFmtXmlEbo xml:xmls){
			System.out.println(xml.getXsdFile());
		}
	}

	// 测试插入XML文件，并读出
	private static void testInitXMLFormat(String fname) throws AppException, Exception {
		// test insert into new XML format
		TCfgFormatEbo format = new TCfgFormatEbo();
		format.setName("行内1_xml");
		format.setFormat("xml");
		TCfgFormatMgrImpl fmtImp = (TCfgFormatMgrImpl) f3.getBean("cfgFormatMgr");
		fmtImp.save(format);

		TCfgFmtXmlEbo xmlFmt = new TCfgFmtXmlEbo();
		xmlFmt.setTCfgFormat(format);
		xmlFmt.setIsFile('N');
		String file = FileUtil.readFile(fname);
		xmlFmt.setXsdFile(file);
		fmtImp.save(xmlFmt);

		/*Session session = fmtImp.getSessionFactory().openSession();
		Transaction tx = session.beginTransaction();

		TCfgFmtXmlEbo xmlFmt = new TCfgFmtXmlEbo();
		xmlFmt.setTCfgFormat(format);
		xmlFmt.setIsFile('N');
		xmlFmt.setXsdFile(Hibernate.createClob(" "));
		session.save(xmlFmt);
		session.flush();
		session.refresh(xmlFmt, LockMode.UPGRADE);
		String file = FileUtil.readFile(fname);

		SerializableClob sclob = (SerializableClob) xmlFmt.getXsdFile();
		java.sql.Clob jclob = sclob.getWrappedClob();
		oracle.sql.CLOB clob = (oracle.sql.CLOB) jclob;
		try {
			Writer out = clob.getCharacterOutputStream();
			out.write(file);
			out.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
		tx.commit();*/
	}
}
